import LogFactory from "log/LogFactory";
import {ReimburseType} from "type/ReimburseType";
import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {uuid32} from "util/uuid";
import {
    fetchAddReimburse,
    fetchDeleteReimburse,
    fetchPageData,
    fetchUpdateReimburse,
    fetchUpdateTotalAmount
} from "./async/reimburseAsyncAction";
import System from "global/System";
import {StateBaseType} from "../StateBaseType";
import {ReduxStatus} from "enum/ReduxStatus";


const log = LogFactory.getLog("Redux.reimburse") ;
export type ReimburseState = StateBaseType<ReimburseType> ;
const initialState: ReimburseState = {
    data: [] ,
    pageSize: System.getInstance().getUserConfig().reimbursePageSize,
    current: 1,
    total: 0,
    loading: false,
    status: ReduxStatus.INIT,
} ;

export const reimburseSlice = createSlice({
    name: 'reimburse',
    initialState,
    reducers: {
        deleteById: (state, action: PayloadAction<number>) => {
        }
    },
    extraReducers: (builder) => {
        /**
         * 获取报销单分页数据前操作
         */
        builder.addCase(fetchPageData.pending, (state) => {
            state.loading = true;
            state.status = 1;
        });
        /**
         * 获取报销单分页数据成功后操作
         */
        builder.addCase(fetchPageData.fulfilled, (state, action) => {
            state.loading = false;
            state.status = 3;
            state.data = action.payload.data;
            state.current = action.payload.currentPage;
            state.pageSize = action.payload.pageSize;
            state.total = action.payload.total;
        });
        /**
         * 获取报销单分页数据失败后操作
         */
        builder.addCase(fetchPageData.rejected, (state, action) => {
            state.loading = false;
            state.status = ReduxStatus.FAILED
        });
        /**
         * 更新报销单总金额和条目数量成功后操作
         */
        builder.addCase(fetchUpdateTotalAmount.fulfilled, (state, action) => {
            if (action.payload.data > 0) {
                // log.redux("更新报销单总金额和条目数量成功: ", action.payload) ;
                const {id, totalAmount, itemCount} = action.payload.reimburse;
                let index = state.data.findIndex(item => item.id === id);
                if (index !== -1) {
                    state.data[index].itemCount = itemCount;
                    state.data[index].totalAmount = totalAmount;
                }
            } else {
                log.redux("更新报销单总金额和条目数量失败: ", action.payload);
            }
        });
        /**
         * 添加报销单成功后操作
         */
        builder.addCase(fetchAddReimburse.fulfilled, (state, action) => {
            if (action.payload.success) {
                log.redux("添加报销单成功: ", action.payload);
                state.data.unshift(action.payload.data);
            } else {
                log.redux("添加报销单失败: ", action.payload);
            }
        });
        /**
         * 更新报销但成功后操作
         */
        builder.addCase(fetchUpdateReimburse.fulfilled, (state, action) => {
            if (action.payload.data > 0) {
                log.redux("更新报销单成功: ", action.payload.data);
                let index = state.data.findIndex(item => item.id === action.payload.reimburse.id);
                if (index !== -1) {
                    action.payload.reimburse.key = uuid32();
                    state.data[index] = action.payload.reimburse;
                }
            } else {
                log.redux("更新报销单失败: ", action.payload);
            }
        });
        /**
         * 删除报销单成功后操作
         */
        builder.addCase(fetchDeleteReimburse.fulfilled, (state, action) => {
            if (action.payload.data > 0){
                log.redux("删除报销单成功: ", action.payload) ;
                state.data = state.data.filter(item => item.id !== action.payload.id) ;
            }else {
                log.redux("删除报销单失败: ", action.payload) ;
            }
        });
    }
});

export const {deleteById} = reimburseSlice.actions ;

export default reimburseSlice.reducer ;